package com.srwl.mytx.im;

import android.content.Context;

import com.srwl.mytx.Constant;
import com.srwl.mytx.domain.User;
import com.srwl.mytx.im.core.NettyTcpClient;
import com.srwl.mytx.im.listener.OnEventListener;
import com.srwl.mytx.im.protobuf.MessageProtobuf;
import com.srwl.mytx.parse.UserProfileManager;
import com.srwl.mytx.utils.IDUtils;
import com.srwl.mytx.utils.NetworkUtil;


/**
 * <p>@ProjectName:     NettyChat</p>
 * <p>@ClassName:       IMSEventListener.java</p>
 * <p>@PackageName:     com.freddy.chat.im</p>
 * <b>
 * <p>@Description:     与ims交互的listener</p>
 * </b>
 * <p>@author:          FreddyChen</p>
 * <p>@date:            2019/04/07 23:55</p>
 * <p>@email:           chenshichao@outlook.com</p>
 */
public class IMSEventListener implements OnEventListener {

    private Context context;
    private User loginUserInfo;

    public IMSEventListener(Context context) {
        this.context = context;
        loginUserInfo = UserProfileManager.getInstance().getLoginUserInfo();
    }


    /**
     * 网络是否可用
     *
     * @return
     */
    @Override
    public boolean isNetworkAvailable() {
        return NetworkUtil.isNetworkAvailable(context);
    }

    /**
     * 设置ims重连间隔时长，0表示默认使用ims的值
     *
     * @return
     */
    @Override
    public int getReconnectInterval() {
        return 0;
    }

    /**
     * 设置ims连接超时时长，0表示默认使用ims的值
     *
     * @return
     */
    @Override
    public int getConnectTimeout() {
        return 0;
    }

    /**
     * 设置应用在前台时ims心跳间隔时长，0表示默认使用ims的值
     *
     * @return
     */
    @Override
    public int getForegroundHeartbeatInterval() {
        return 20;
    }

    /**
     * 设置应用在后台时ims心跳间隔时长，0表示默认使用ims的值
     *
     * @return
     */
    @Override
    public int getBackgroundHeartbeatInterval() {
        return 40;
    }

    /**
     * 构建连接消息
     *
     * @return
     */
    @Override
    public MessageProtobuf.Msg getHandshakeMsg() {
        MessageProtobuf.Msg.Builder builder = MessageProtobuf.Msg.newBuilder();
        MessageProtobuf.Head.Builder headBuilder = MessageProtobuf.Head.newBuilder();
        headBuilder.setMsgId(IDUtils.genOrderItemId());
        headBuilder.setMsgType(Constant.IM_TYPE_CONNECT);
        headBuilder.setFromId(loginUserInfo.getuId());
        //重连的时候必须携带登录时间上去
        headBuilder.setTimestamp(NettyTcpClient.getInstance().getLoginTimeStamp());
        builder.setHead(headBuilder.build());
        return builder.build();
    }

    @Override
    public MessageProtobuf.Msg getLoginMsg() {

        MessageProtobuf.Msg.Builder builder = MessageProtobuf.Msg.newBuilder();
        MessageProtobuf.Head.Builder headBuilder = MessageProtobuf.Head.newBuilder();
        headBuilder.setMsgId(IDUtils.genOrderItemId());
        headBuilder.setMsgType(Constant.IM_TYPE_LOGIN);
        headBuilder.setFromId(loginUserInfo.getuId());
        headBuilder.setTimestamp(System.currentTimeMillis());
        headBuilder.setExtend(UserProfileManager.getInstance().getLoginUserInfo().getToken());
        builder.setHead(headBuilder.build());
        return builder.build();
    }

    /**
     * 构建心跳消息
     *
     * @return
     */
    @Override
    public MessageProtobuf.Msg getHeartbeatMsg() {
        MessageProtobuf.Msg.Builder builder = MessageProtobuf.Msg.newBuilder();
        MessageProtobuf.Head.Builder headBuilder = MessageProtobuf.Head.newBuilder();
        headBuilder.setMsgId(IDUtils.genOrderItemId());
        headBuilder.setMsgType(Constant.IM_TYPE_HEAT_BRAT);
        headBuilder.setFromId(loginUserInfo.getuId());
        headBuilder.setTimestamp(System.currentTimeMillis());
        builder.setHead(headBuilder.build());
        return builder.build();

    }


    /**
     * 设置ims消息发送超时重发次数，0表示默认使用ims的值
     *
     * @return
     */
    @Override
    public int getResendCount() {
        return 0;
    }

    /**
     * 设置ims消息发送超时重发间隔时长，0表示默认使用ims的值
     *
     * @return
     */
    @Override
    public int getResendInterval() {
        return 0;
    }
}
